<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="Pandas学习笔记02">
<meta name="keywords" content="学习笔记,Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Pandas学习笔记02">
<meta property="og:url" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2021&#x2F;03&#x2F;27&#x2F;pandas02&#x2F;index.html">
<meta property="og:site_name" content="付文杰的博客">
<meta property="og:description" content="Pandas学习笔记02">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2021&#x2F;03&#x2F;27&#x2F;pandas02&#x2F;1616810927513.png">
<meta property="og:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2021&#x2F;03&#x2F;27&#x2F;pandas02&#x2F;1616811967342.png">
<meta property="og:updated_time" content="2021-03-30T13:09:37.740Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2021&#x2F;03&#x2F;27&#x2F;pandas02&#x2F;1616810927513.png">

<link rel="canonical" href="https://fwj1635387072.github.io/2021/03/27/pandas02/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>


  <title>Pandas学习笔记02 | 付文杰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">付文杰的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">个人博客  |  日常</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>

      </div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://FWj1635387072.github.io/2021/03/27/pandas02/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/headImage.jpg">
      <meta itemprop="name" content="付文杰">
      <meta itemprop="description" content="兰州交通大学 | 本科生 <br> 信息管理与信息系统">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="付文杰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Pandas学习笔记02
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-27 09:46:22" itemprop="dateCreated datePublished" datetime="2021-03-27T09:46:22+08:00">2021-03-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-03-30 21:09:37" itemprop="dateModified" datetime="2021-03-30T21:09:37+08:00">2021-03-30</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Python/" itemprop="url" rel="index">
                    <span itemprop="name">Python</span>
                  </a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>5.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>10 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <pre><code>Pandas学习笔记02</code></pre><a id="more"></a>

<h1 id="分组"><a href="#分组" class="headerlink" title="分组"></a>分组</h1><p>分组要确定：分组依据，数据来源、操作及返回结果。</p>
<p><code>df.groupby(分组依据)[数据来源].操作</code></p>
<p>分组的三大操作，聚合、变换、过滤</p>
<p>groupby对象中<code>transform</code>、<code>filter</code>、<code>apply</code>方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">df = pd.read_csv(<span class="string">'learn_pandas.csv'</span>)</span><br><span class="line">df.drop([<span class="string">'School'</span>,<span class="string">'Name'</span>] ,axis=<span class="number">1</span>,inplace=<span class="literal">True</span>)</span><br><span class="line">df.head(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">df.groupby([<span class="string">'Grade'</span>,<span class="string">'Gender'</span>])[[<span class="string">'Height'</span>,<span class="string">'Weight'</span>]].mean()</span><br></pre></td></tr></table></figure>

<p><img src="/2021/03/27/pandas02/1616810927513.png" alt="1616810927513"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gb.agg(&#123;<span class="string">'Height'</span>:[<span class="string">'mean'</span>,<span class="string">'max'</span>],</span><br><span class="line">        <span class="string">'Weight'</span>:[<span class="string">'mean'</span>,<span class="string">'max'</span>]&#125;)</span><br></pre></td></tr></table></figure>

<p><img src="/2021/03/27/pandas02/1616811967342.png" alt="1616811967342"></p>
<p>聚合结果重命名：将字典换位元组,第一个参数为名称，第二个为方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gb.agg([(<span class="string">'range'</span>,<span class="keyword">lambda</span> x:x.max() - x.min()),	</span><br><span class="line">        (<span class="string">'my_sum'</span>,<span class="string">'sum'</span>)])</span><br></pre></td></tr></table></figure>

<p>归一化</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">gb = df.groupby(<span class="string">'Type'</span>)[<span class="string">'HP'</span>]</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">normalize</span><span class="params">(s)</span>:</span></span><br><span class="line">    s_min,s_max = s.min(),s.max()</span><br><span class="line">    res = (s - s_min)/(s_max - s_min)</span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line">gb.apply(normalize)</span><br></pre></td></tr></table></figure>

<h1 id="变形"><a href="#变形" class="headerlink" title="变形"></a>变形</h1><p><code>pivot</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">df = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">'Class'</span>:[<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>],</span><br><span class="line">    <span class="string">'Name'</span>:[<span class="string">'zhangsan'</span>,<span class="string">'zhangsan'</span>,<span class="string">'lisi'</span>,<span class="string">'lisi'</span>],</span><br><span class="line">    <span class="string">'Subject'</span>:[<span class="string">'Chinese'</span>,<span class="string">'Math'</span>,<span class="string">'Chinese'</span>,<span class="string">'Math'</span>],</span><br><span class="line">    <span class="string">'Grade'</span>:[<span class="number">80</span>,<span class="number">75</span>,<span class="number">90</span>,<span class="number">85</span>]</span><br><span class="line">&#125;)</span><br><span class="line">df</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">df.pivot(index=<span class="string">'Name'</span>,columns=<span class="string">'Subject'</span>,values=<span class="string">'Grade'</span>)</span><br></pre></td></tr></table></figure>

<p><code>pivot_table</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">df = pd.DataFrame(&#123;<span class="string">'Class'</span>:[<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>],</span><br><span class="line">                   <span class="string">'Name'</span>:[<span class="string">'San Zhang'</span>, <span class="string">'San Zhang'</span>, <span class="string">'Si Li'</span>, <span class="string">'Si Li'</span>,</span><br><span class="line">                              <span class="string">'San Zhang'</span>, <span class="string">'San Zhang'</span>, <span class="string">'Si Li'</span>, <span class="string">'Si Li'</span>],</span><br><span class="line">                   <span class="string">'Examination'</span>: [<span class="string">'Mid'</span>, <span class="string">'Final'</span>, <span class="string">'Mid'</span>, <span class="string">'Final'</span>,</span><br><span class="line">                                    <span class="string">'Mid'</span>, <span class="string">'Final'</span>, <span class="string">'Mid'</span>, <span class="string">'Final'</span>],</span><br><span class="line">                   <span class="string">'Subject'</span>:[<span class="string">'Chinese'</span>, <span class="string">'Chinese'</span>, <span class="string">'Chinese'</span>, <span class="string">'Chinese'</span>,</span><br><span class="line">                                 <span class="string">'Math'</span>, <span class="string">'Math'</span>, <span class="string">'Math'</span>, <span class="string">'Math'</span>],</span><br><span class="line">                   <span class="string">'Grade'</span>:[<span class="number">80</span>, <span class="number">75</span>, <span class="number">85</span>, <span class="number">65</span>, <span class="number">90</span>, <span class="number">85</span>, <span class="number">92</span>, <span class="number">88</span>],</span><br><span class="line">                   <span class="string">'rank'</span>:[<span class="number">10</span>, <span class="number">15</span>, <span class="number">21</span>, <span class="number">15</span>, <span class="number">20</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">2</span>]&#125;)</span><br><span class="line">df</span><br><span class="line"></span><br><span class="line">pivot_multi = df.pivot(index=[<span class="string">'Class'</span>,<span class="string">'Name'</span>],</span><br><span class="line">                      columns=[<span class="string">'Subject'</span>,<span class="string">'Examination'</span>],</span><br><span class="line">                      values=[<span class="string">'Grade'</span>,<span class="string">'rank'</span>])</span><br><span class="line">pivot_multi</span><br></pre></td></tr></table></figure>

<p><code>pivot_table</code>中<code>margins</code></p>
<p>汇总</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">df.pivot_table(index=<span class="string">'Name'</span>,</span><br><span class="line">              columns=<span class="string">'Subject'</span>,</span><br><span class="line">              values=<span class="string">'Grade'</span>,</span><br><span class="line">              aggfunc=<span class="string">'mean'</span>,</span><br><span class="line">              margins=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>

<h1 id="缺失值处理"><a href="#缺失值处理" class="headerlink" title="缺失值处理"></a>缺失值处理</h1><h2 id="1-缺失值的查看"><a href="#1-缺失值的查看" class="headerlink" title="1.缺失值的查看"></a>1.缺失值的查看</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">df = pd.read_csv(<span class="string">'learn_pandas.csv'</span>,</span><br><span class="line">                 usecols=[<span class="string">'Grade'</span>,<span class="string">'Name'</span>,<span class="string">'Gender'</span>,<span class="string">'Height'</span>,<span class="string">'Weight'</span>,<span class="string">'Transfer'</span>])</span><br><span class="line">df.head()</span><br><span class="line"></span><br><span class="line"><span class="comment">#查看哪些属性有缺失</span></span><br><span class="line">df.isna().mean()</span><br><span class="line"></span><br><span class="line"><span class="comment">#查看Height属性缺失的行</span></span><br><span class="line">df[df.Height.isna()].head()</span><br><span class="line"></span><br><span class="line"><span class="comment">#查看全部缺失的</span></span><br><span class="line">sub_set = df[[<span class="string">'Height'</span>,<span class="string">'Weight'</span>,<span class="string">'Transfer'</span>]]</span><br><span class="line">df[sub_set.isna().all(<span class="number">1</span>)]</span><br><span class="line"><span class="comment">#查看任意缺失一个的</span></span><br><span class="line">df[sub_set.isna().any(<span class="number">1</span>)].head()</span><br><span class="line"><span class="comment">#查看没有缺失的</span></span><br><span class="line">df[sub_set.notna().all(<span class="number">1</span>)].head()</span><br></pre></td></tr></table></figure>

<h2 id="2-缺失信息的删除"><a href="#2-缺失信息的删除" class="headerlink" title="2. 缺失信息的删除"></a>2. 缺失信息的删除</h2><p>数据处理中经常需要根据缺失值的大小、比例或其他特征来进行行样本或列特征的删除，<code>pandas</code>中提供了<code>dropna</code>函数来进行操作。</p>
<p><code>dropna</code>的主要参数为轴方向<code>axis</code>（默认为0，即删除行）、删除方式<code>how</code>、删除的非缺失值个数阈值<code>thresh</code>（<strong>非缺失值</strong>没有达到这个数量的相应维度会被删除）、备选的删除子集<code>subset</code>，其中<code>how</code>主要有<code>any</code>和<code>all</code>两种参数可以选择。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#删除身高体重至少有一个缺失的行：</span></span><br><span class="line">res = df.dropna(how = <span class="string">'any'</span>,</span><br><span class="line">               subset=[<span class="string">'Height'</span>,<span class="string">'Weight'</span>])</span><br><span class="line">res.shape</span><br><span class="line"></span><br><span class="line"><span class="comment">#删除缺失值超过15的列</span></span><br><span class="line">res = df.dropna(<span class="number">1</span>,thresh=df.shape[<span class="number">0</span>]<span class="number">-15</span>)</span><br><span class="line">res.head()</span><br></pre></td></tr></table></figure>

<h2 id="3-缺失值的填充"><a href="#3-缺失值的填充" class="headerlink" title="3.缺失值的填充"></a>3.缺失值的填充</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#fillna()，value 填充值，method填充方法，limit 连续缺失值的最大填充次数</span></span><br><span class="line">s = pd.Series([np.nan, <span class="number">1</span>, np.nan, np.nan, <span class="number">2</span>, np.nan], list(<span class="string">'aaabcd'</span>))</span><br><span class="line"><span class="comment">#ffill 用后面的值填充到前面</span></span><br><span class="line">s.fillna(method=<span class="string">'ffill'</span>)</span><br><span class="line"><span class="comment">#用均值填充</span></span><br><span class="line">s.fillna(s.mean())</span><br><span class="line"><span class="comment">#有时候为了更合理，先分组后填充</span></span><br><span class="line">df.groupby(<span class="string">'Grade'</span>)[<span class="string">'Height'</span>].transform(<span class="keyword">lambda</span> x:x.fillna(x.mean())).head()</span><br></pre></td></tr></table></figure>

<h2 id="2-插值函数"><a href="#2-插值函数" class="headerlink" title="2. 插值函数"></a>2. 插值函数</h2><p>​    在关于<code>interpolate</code>函数的<a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.interpolate.html#pandas.Series.interpolate" target="_blank" rel="noopener">文档</a>描述中，列举了许多插值法，包括了大量<code>Scipy</code>中的方法。由于很多插值方法涉及到比较复杂的数学知识，因此这里只讨论比较常用且简单的三类情况，即线性插值、最近邻插值和索引插值。</p>
<p>​    对于<code>interpolate</code>而言，除了插值方法（默认为<code>linear</code>线性插值）之外，有与<code>fillna</code>类似的两个常用参数，一个是控制方向的<code>limit_direction</code>，另一个是控制最大连续缺失值插值个数的<code>limit</code>。其中，限制插值的方向默认为<code>forward</code>，这与<code>fillna</code>的<code>method</code>中的<code>ffill</code>是类似的，若想要后向限制插值或者双向限制插值可以指定为<code>backward</code>或<code>both</code>。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">s = pd.Series([np.nan, np.nan, <span class="number">1</span>, np.nan, np.nan, np.nan, <span class="number">2</span>, np.nan, np.nan])</span><br><span class="line">s</span><br><span class="line"><span class="comment">#在默认线性插值法下分别进行`backward`和双向限制插值，同时限制最大连续条数为1：</span></span><br><span class="line">res = s.interpolate(limit_direction=<span class="string">'backward'</span>,limit=<span class="number">1</span>)</span><br><span class="line">res</span><br><span class="line">res = s.interpolate(limit_direction=<span class="string">'both'</span>,limit=<span class="number">1</span>)</span><br><span class="line">res</span><br></pre></td></tr></table></figure>

<p>第二种常见的插值是最近邻插补，即缺失值的元素和离它最近的非缺失值元素一样：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s.interpolate(<span class="string">'nearest'</span>)</span><br></pre></td></tr></table></figure>

<p>最后来介绍索引插值，即根据索引大小进行线性插值。例如，构造不等间距的索引进行演示：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">s = pd.Series([<span class="number">0</span>,np.nan,<span class="number">10</span>],index=[<span class="number">0</span>,<span class="number">1</span>,<span class="number">10</span>])</span><br><span class="line">s</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#默认线性插值，等价于计算中点的值</span></span><br><span class="line">s.interpolate()</span><br><span class="line"><span class="comment"># 和索引有关的线性插值，计算相应索引大小对应的值</span></span><br><span class="line">s.interpolate(method=<span class="string">'index'</span>)</span><br></pre></td></tr></table></figure>

<h1 id="分类数据"><a href="#分类数据" class="headerlink" title="分类数据"></a>分类数据</h1><h2 id="一、cat对象"><a href="#一、cat对象" class="headerlink" title="一、cat对象"></a>一、cat对象</h2><h3 id="1-cat对象的属性"><a href="#1-cat对象的属性" class="headerlink" title="1. cat对象的属性"></a>1. cat对象的属性</h3><p>在<code>pandas</code>中提供了<code>category</code>类型，使用户能够处理分类类型的变量，将一个普通序列转换成分类变量可以使用<code>astype</code>方法。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">df = pd.read_csv(<span class="string">'learn_pandas.csv'</span>,</span><br><span class="line">                 usecols=[<span class="string">'Grade'</span>,<span class="string">'Name'</span>,<span class="string">'Gender'</span>,<span class="string">'Height'</span>,<span class="string">'Weight'</span>])</span><br><span class="line">s = df.Grade.astype(<span class="string">'category'</span>)</span><br><span class="line">s.head()</span><br></pre></td></tr></table></figure>

<p>每一个序列的类别会被赋予唯一的整数编号，它们的编号取决于<code>cat.categories</code>中的顺序，该属性可以通过<code>codes</code>访问：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s.cat.codes.head()</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">s = s.cat.add_categories(<span class="string">'Graduate'</span>)</span><br><span class="line">s = s.cat.remove_categories(<span class="string">'Freshman'</span>)</span><br></pre></td></tr></table></figure>

<h2 id="二、有序分类"><a href="#二、有序分类" class="headerlink" title="二、有序分类"></a>二、有序分类</h2><h3 id="1-序的建立"><a href="#1-序的建立" class="headerlink" title="1. 序的建立"></a>1. 序的建立</h3><p>​    有序类别和无序类别可以通过<code>as_unordered</code>和<code>reorder_categories</code>互相转化，需要注意的是后者传入的参数必须是由当前序列的无序类别构成的列表，不能够增加新的类别，也不能缺少原来的类别，并且必须指定参数<code>ordered=True</code>，否则方法无效。例如，对年级高低进行相对大小的类别划分，然后再恢复无序状态：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s = df.Grade.astype(<span class="string">'category'</span>)</span><br><span class="line">s = s.cat.reorder_categories([<span class="string">'Freshman'</span>, <span class="string">'Sophomore'</span>, <span class="string">'Junior'</span>, <span class="string">'Senior'</span>],ordered=<span class="literal">True</span>)</span><br><span class="line">s.head()</span><br></pre></td></tr></table></figure>

<p>排序</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">df.Grade = df.Grade.astype(<span class="string">'category'</span>)</span><br><span class="line">df.Grade = df.Grade.cat.reorder_categories([<span class="string">'Freshman'</span>,<span class="string">'Sophomore'</span>,<span class="string">'Junior'</span>,<span class="string">'Senior'</span>],</span><br><span class="line">                                           ordered=<span class="literal">True</span>)</span><br><span class="line">df.sort_values(<span class="string">'Grade'</span>).head()</span><br></pre></td></tr></table></figure>

<h2 id="三、区间类别"><a href="#三、区间类别" class="headerlink" title="三、区间类别"></a>三、区间类别</h2><h3 id="1-利用cut和qcut进行区间构造"><a href="#1-利用cut和qcut进行区间构造" class="headerlink" title="1. 利用cut和qcut进行区间构造"></a>1. 利用cut和qcut进行区间构造</h3><p>​        区间是一种特殊的类别，在实际数据分析中，区间序列往往是通过<code>cut</code>和<code>qcut</code>方法进行构造的，这两个函数能够把原序列的数值特征进行装箱，即用区间位置来代替原来的具体数值。</p>
<p>首先介绍<code>cut</code>的常见用法：</p>
<p>​        其中，最重要的参数是<code>bins</code>，如果传入整数<code>n</code>，则代表把整个传入数组的按照最大和最小值等间距地分为<code>n</code>段。由于区间默认是左开右闭，需要在调整时把最小值包含进去，在<code>pandas</code>中的解决方案是在值最小的区间左端点再减去<code>0.001*(max-min)</code>，因此如果对序列<code>[1,2]</code>划分为2个箱子时，第一个箱子的范围<code>(0.999,1.5]</code>，第二个箱子的范围是<code>(1.5,2]</code>。如果需要指定左闭右开时，需要把<code>right</code>参数设置为<code>False</code>，相应的区间调整方法是在值最大的区间右端点再加上<code>0.001*(max-min)</code>。</p>
<p>…</p>
<h2 id="一、时序中的基本对象"><a href="#一、时序中的基本对象" class="headerlink" title="一、时序中的基本对象"></a>一、时序中的基本对象</h2><p>时间序列的概念在日常生活中十分常见，但对于一个具体的时序事件而言，可以从多个时间对象的角度来描述。例如2020年9月7日周一早上8点整需要到教室上课，这个课会在当天早上10点结束，其中包含了哪些时间概念？</p>
<ul>
<li><p>第一，会出现时间戳（Date times）的概念，即’2020-9-7 08:00:00’和’2020-9-7 10:00:00’这两个时间点分别代表了上课和下课的时刻，在<code>pandas</code>中称为<code>Timestamp</code>。同时，一系列的时间戳可以组成<code>DatetimeIndex</code>，而将它放到<code>Series</code>中后，<code>Series</code>的类型就变为了<code>datetime64[ns]</code>，如果有涉及时区则为<code>datetime64[ns, tz]</code>，其中tz是timezone的简写。</p>
</li>
<li><p>第二，会出现时间差（Time deltas）的概念，即上课需要的时间，两个<code>Timestamp</code>做差就得到了时间差，pandas中利用<code>Timedelta</code>来表示。类似的，一系列的时间差就组成了<code>TimedeltaIndex</code>， 而将它放到<code>Series</code>中后，<code>Series</code>的类型就变为了<code>timedelta64[ns]</code>。</p>
</li>
<li><p>第三，会出现时间段（Time spans）的概念，即在8点到10点这个区间都会持续地在上课，在<code>pandas</code>利用<code>Period</code>来表示。类似的，一系列的时间段就组成了<code>PeriodIndex</code>， 而将它放到<code>Series</code>中后，<code>Series</code>的类型就变为了<code>Period</code>。</p>
</li>
<li><p>第四，会出现日期偏置（Date offsets）的概念，假设你只知道9月的第一个周一早上8点要去上课，但不知道具体的日期，那么就需要一个类型来处理此类需求。再例如，想要知道2020年9月7日后的第30个工作日是哪一天，那么时间差就解决不了你的问题，从而<code>pandas</code>中的<code>DateOffset</code>就出现了。同时，<code>pandas</code>中没有为一列时间偏置专门设计存储类型，理由也很简单，因为需求比较奇怪，一般来说我们只需要对一批时间特征做一个统一的特殊日期偏置。</p>
</li>
</ul>
<p>通过这个简单的例子，就能够容易地总结出官方文档中的这个<a href="https://pandas.pydata.org/docs/user_guide/timeseries.html#overview" target="_blank" rel="noopener">表格</a>：</p>
<table>
<thead>
<tr>
<th align="left">概念</th>
<th align="left">单元素类型</th>
<th align="left">数组类型</th>
<th align="left">pandas数据类型</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Date times</td>
<td align="left"><code>Timestamp</code></td>
<td align="left"><code>DatetimeIndex</code></td>
<td align="left"><code>datetime64[ns]</code></td>
</tr>
<tr>
<td align="left">Time deltas</td>
<td align="left"><code>Timedelta</code></td>
<td align="left"><code>TimedeltaIndex</code></td>
<td align="left"><code>timedelta64[ns]</code></td>
</tr>
<tr>
<td align="left">Time spans</td>
<td align="left"><code>Period</code></td>
<td align="left"><code>PeriodIndex</code></td>
<td align="left"><code>period[freq]</code></td>
</tr>
<tr>
<td align="left">Date offsets</td>
<td align="left"><code>DateOffset</code></td>
<td align="left"><code>None</code></td>
<td align="left"><code>None</code></td>
</tr>
</tbody></table>
<p>由于时间段对象<code>Period/PeriodIndex</code>的使用频率并不高，因此将不进行讲解，而只涉及时间戳序列、时间差序列和日期偏置的相关内容。</p>
<h2 id="二、时间戳"><a href="#二、时间戳" class="headerlink" title="二、时间戳"></a>二、时间戳</h2><h3 id="1-Timestamp的构造与属性"><a href="#1-Timestamp的构造与属性" class="headerlink" title="1. Timestamp的构造与属性"></a>1. Timestamp的构造与属性</h3><p>单个时间戳的生成利用<code>pd.Timestamp</code>实现，一般而言的常见日期格式都能被成功地转换：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ts = pd.Timestamp(<span class="string">'2020/1/1'</span>) </span><br><span class="line">ts</span><br><span class="line"></span><br><span class="line">ts = pd.Timestamp(<span class="string">'2020-1-1 08:10:30'</span>)</span><br><span class="line">ts</span><br></pre></td></tr></table></figure>

<p><code>date_range</code>是一种生成连续间隔时间的一种方法，其重要的参数为<code>start, end, freq, periods</code>，它们分别表示开始时间，结束时间，时间间隔，时间戳个数。其中，四个中的三个参数决定了，那么剩下的一个就随之确定了。这里要注意，开始或结束日期如果作为端点则它会被包含：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pd.date_range(<span class="string">'2020-1-1'</span>,<span class="string">'2020-1-21'</span>,freq=<span class="string">'2D'</span>)</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      
      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
        
      </div>

      <footer class="post-footer">



        
<div>    
 
 
<ul class="post-copyright">
  <li class="post-copyright-author">
      <strong>本文作者：</strong>付文杰
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="/2021/03/27/pandas02/" title="Pandas学习笔记02">2021/03/27/pandas02/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权： </strong>
    本站文章均采用 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0 CN</a> 许可协议，请勿用于商业，转载注明出处！
  </li>
</ul>

</div>
          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"><i class="fa fa-tag"></i> 学习笔记</a>
              <a href="/tags/Python/" rel="tag"><i class="fa fa-tag"></i> Python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2021/03/14/pandas/" rel="next" title="Pandas学习笔记01">
                  <i class="fa fa-chevron-left"></i> Pandas学习笔记01
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
                <a href="/2021/03/30/Untitled%201/" rel="prev" title="Untitled 1">
                  Untitled 1 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=27588222&auto=1&height=66"></iframe>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#分组"><span class="nav-number">1.</span> <span class="nav-text">分组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#变形"><span class="nav-number">2.</span> <span class="nav-text">变形</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#缺失值处理"><span class="nav-number">3.</span> <span class="nav-text">缺失值处理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-缺失值的查看"><span class="nav-number">3.1.</span> <span class="nav-text">1.缺失值的查看</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-缺失信息的删除"><span class="nav-number">3.2.</span> <span class="nav-text">2. 缺失信息的删除</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-缺失值的填充"><span class="nav-number">3.3.</span> <span class="nav-text">3.缺失值的填充</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-插值函数"><span class="nav-number">3.4.</span> <span class="nav-text">2. 插值函数</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#分类数据"><span class="nav-number">4.</span> <span class="nav-text">分类数据</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#一、cat对象"><span class="nav-number">4.1.</span> <span class="nav-text">一、cat对象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-cat对象的属性"><span class="nav-number">4.1.1.</span> <span class="nav-text">1. cat对象的属性</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、有序分类"><span class="nav-number">4.2.</span> <span class="nav-text">二、有序分类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-序的建立"><span class="nav-number">4.2.1.</span> <span class="nav-text">1. 序的建立</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、区间类别"><span class="nav-number">4.3.</span> <span class="nav-text">三、区间类别</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-利用cut和qcut进行区间构造"><span class="nav-number">4.3.1.</span> <span class="nav-text">1. 利用cut和qcut进行区间构造</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一、时序中的基本对象"><span class="nav-number">4.4.</span> <span class="nav-text">一、时序中的基本对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、时间戳"><span class="nav-number">4.5.</span> <span class="nav-text">二、时间戳</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-Timestamp的构造与属性"><span class="nav-number">4.5.1.</span> <span class="nav-text">1. Timestamp的构造与属性</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="付文杰"
    src="/images/headImage.jpg">
  <p class="site-author-name" itemprop="name">付文杰</p>
  <div class="site-description" itemprop="description">兰州交通大学 | 本科生 <br> 信息管理与信息系统</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">63</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/FWj1635387072" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;FWj1635387072" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:1635387072@qq.com" title="E-Mail &amp;rarr; mailto:1635387072@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/null" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>





      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        


<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">付文杰</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">210k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:50</span>

  <br>
  <span id="sitetime"></span>

</div>
<script language=javascript>
    function siteTime() {
        window.setTimeout("siteTime()", 1000);
        var seconds = 1000;
        var minutes = seconds * 60;
        var hours = minutes * 60;
        var days = hours * 24;
        var years = days * 365;
        var today = new Date();
        var todayYear = today.getFullYear();
        var todayMonth = today.getMonth() + 1;
        var todayDate = today.getDate();
        var todayHour = today.getHours();
        var todayMinute = today.getMinutes();
        var todaySecond = today.getSeconds();
        /* Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
        year - 作为date对象的年份，为4位年份值
        month - 0-11之间的整数，做为date对象的月份
        day - 1-31之间的整数，做为date对象的天数
        hours - 0(午夜24点)-23之间的整数，做为date对象的小时数
        minutes - 0-59之间的整数，做为date对象的分钟数
        seconds - 0-59之间的整数，做为date对象的秒数
        microseconds - 0-999之间的整数，做为date对象的毫秒数 */
        var t1 = Date.UTC(2019, 11, 7, 17, 50, 00); //北京时间2018-2-13 00:00:00
        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
        var diff = t2 - t1;
        var diffYears = Math.floor(diff / years);
        var diffDays = Math.floor((diff / days) - diffYears * 365);
        var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
        var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes);
        var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours - diffMinutes * minutes) / seconds);
        document.getElementById("sitetime").innerHTML = "本站已运行 " +diffYears+" 年 "+diffDays + " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
    }/*因为建站时间还没有一年，就将之注释掉了。需要的可以取消*/
    siteTime();
</script>


        












        
      </div>
    </footer>
  </div>

  
  
  <script color='0,0,255' opacity='0.5' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  
















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"symbols_count_time":null,"symbols":true,"time":true,"total_symbols":true,"total_time":true,"log":false});</script></body>
</html>
<script type="text/javascript" src="/js/src/clicklove.js"></script>